package cn.edu.scau.cmi.xiehaizhou.composite.safe;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class CilentController implements Initializable{
	
	Organization nowOrganization;
	
	@FXML
    private ComboBox<String> cbxChooseOp;

    @FXML
    private Button BtnRun;

    @FXML
    private TextField txtChooseFile;
    
    @FXML
    private TextArea txtArea;

    @FXML
    void BtnRun_Click(ActionEvent event) {
    	switch (cbxChooseOp.getSelectionModel().getSelectedItem()) {
		case "新建个人":
			newPerson();
			break;
		case "新建组织":
			newOrganization();
			break;
		case "遍历当前组织":
			getChildren();
			break;
		case "删除":
			delete();
			break;
		case "进入下一级组织":
			intoChild();
			break;
		case "返回上一级组织":
			intoFather();
			break;
		default:
			break;
		}
    	txtArea.appendText("\n\n当前组织是：" + nowOrganization.getName());
    }

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		cbxChooseOp.getItems().addAll("新建组织","新建个人","删除","遍历当前组织","进入下一级组织","返回上一级组织");
		nowOrganization = new Organization("根组织", null);
	}
	
	public void newPerson()
	{
		if(txtChooseFile.getText().length()==0)
		{
			txtArea.setText("请输入个人名字");
			return;
		}
		String name = txtChooseFile.getText();
		for (OrganizationOrPerson OorP : nowOrganization.childrenList) {
			if(OorP.getName().equals(name))
			{
				txtArea.setText("已有同名组织或个人");
				return;
			}
		}
		nowOrganization.AddPerson(name);
		txtArea.setText("个人"+name+"新建成功");
	}
	public void newOrganization()
	{
		if(txtChooseFile.getText().length()==0)
		{
			txtArea.setText("请输入组织名字");
			return;
		}
		String name = txtChooseFile.getText();
		for (OrganizationOrPerson OorP : nowOrganization.childrenList) {
			if(OorP.getName().equals(name))
			{
				txtArea.setText("已有同名组织或个人");
				return;
			}
		}
		nowOrganization.AddOrganization(name);
		txtArea.setText("组织"+name+"新建成功");
	}
	public void getChildren()
	{
		String reString="";
		if(nowOrganization.childrenList!=null)
			for (OrganizationOrPerson OorP : nowOrganization.childrenList) {
				if(OorP instanceof Organization)
					reString += "组织：" + OorP.getName() + "\n";
				else if(OorP instanceof Person)
					reString += "个人：" + OorP.getName() + "\n";
			}
		txtArea.setText(reString);
	}
	public void delete()
	{
		String name = txtChooseFile.getText();
		if(name.length()==0)
		{
			txtArea.setText("请输入组织或个人名字");
			return;
		}
		nowOrganization.Delete(name);
		txtArea.setText(name+"删除成功");
	}
	public void intoChild()
	{
		boolean contain = false;
		String name = txtChooseFile.getText();
		for (OrganizationOrPerson OorP : nowOrganization.childrenList) {
			if(OorP.getName().equals(name) && OorP instanceof Organization)
			{
				nowOrganization = (Organization)OorP;
				txtArea.setText("成功进入组织"+OorP.getName());
				contain = true;
				break;
			}
		}
		if(!contain)
		{
			txtArea.setText("不存在该组织");
			return;
		}
	}
	public void intoFather()
	{
		if(nowOrganization.fatherOrganization==null)
		{
			txtArea.setText("已到最高级组织");
			return;
		}
		nowOrganization = nowOrganization.fatherOrganization;
		txtArea.setText("成功返回上级组织："+nowOrganization.getName());
	}
}
